package leetcode_day._2021._202112._1120;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/12/13 9:16
 * 保持城市天际线
 * 题目意思就是每一行和每一列不能超过最大的那个数
 * 算法：贪心
 * 每个元素受它所在行和列中的最大值影响，取行列两个最大值中的最小一个
 */
public class _13_807 {
    public static void main(String[] args) {
        int[][] grid = new int[][]{{3,0,8,4},{2,4,5,7},{9,2,6,3},{0,3,1,0}};
        System.out.println(maxIncreaseKeepingSkyline(grid));
    }

    public static int maxIncreaseKeepingSkyline(int[][] grid) {
        int len = grid.length;
        int[] lMax = new int[len], cMax = new int[len];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                lMax[i] = Math.max(lMax[i], grid[i][j]);
                cMax[j] = Math.max(cMax[j], grid[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                ans += Math.min(lMax[i], cMax[j]) - grid[i][j];
            }
        }
        return ans;
    }

}
